﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using mfe.bll;

namespace mfe_proj.Forms
{
    public partial class MagazineForm : BaseDetailForm
    {
        ItemMagazine magazine = null;
        string _ItemGuidID;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public MagazineForm()
        {
            InitializeComponent();
        }

        public Panel DockablePanel
        {
            get { return panel1; }
            set { panel1 = value; }
        }

        void LoadPublisherList()
        {
            if (Program.PublisherCollection == null) Program.PublisherCollection = Publisher.PublisherCollection(null, null);

            cbPublisher.DataSource = Program.PublisherCollection;
            cbPublisher.DisplayMember = "Name";
            cbPublisher.ValueMember = "PublisherGuidID";
            cbPublisher.SelectedIndex = -1;
        }

        public void LoadData()
        {
            LoadPublisherList();

            //edit
            if (ItemGuidID != null && ItemGuidID != string.Empty)
            {
                magazine = new ItemMagazine(new Guid(ItemGuidID));
                if (magazine.PublishedYear > 0)
                    ymdPublishDate.Year = magazine.PublishedYear;
                if (magazine.PublishedMonth > 0)
                    ymdPublishDate.Month = magazine.PublishedMonth;
                if (magazine.PublishedDay > 0)
                    ymdPublishDate.Day = magazine.PublishedDay;
                txtEditor.Text = magazine.Editor;
                txtPreface.Text = magazine.Preface;
                txtPrice.Text = magazine.Price;
                if (magazine.PublisherID != null && magazine.PublisherID != string.Empty)
                    cbPublisher.SelectedValue = magazine.PublisherID;
                LoadTocs();
            }
        }


        public override string ValidateData()
        {
            StringBuilder sb = new StringBuilder();
            if (cbPublisher.SelectedIndex == -1)
                sb.AppendLine("Publisher needs to be selected.");

            return sb.ToString();
        }

        public override bool SaveData(string itemGuidID)
        {
            bool retval = false;
            try
            {
                if (magazine == null || magazine.MagazineID == 0) magazine = new ItemMagazine();
                magazine.ItemGuidID = itemGuidID;

                if (ymdPublishDate.Year.HasValue)
                    magazine.PublishedYear = ymdPublishDate.Year.Value;
                if (ymdPublishDate.Month.HasValue)
                    magazine.PublishedMonth = ymdPublishDate.Month.Value;
                if (ymdPublishDate.Day.HasValue)
                    magazine.PublishedDay = ymdPublishDate.Day.Value;
                magazine.Editor = txtEditor.Text.Trim();
                magazine.Price = txtPrice.Text.Trim();
                magazine.Preface = txtPreface.Text.Trim();
                magazine.PageCount = (int)intPageCount.Value;
                if (cbPublisher.SelectedIndex != -1)
                    magazine.PublisherID = cbPublisher.SelectedValue.ToString();
                //save
                retval = (magazine.MagazineID == 0) ? magazine.Save() : magazine.Update();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retval;
        }

        private void LoadTocs()
        {
            StringCollection conditions = new StringCollection();
            conditions.Add("ItemGuidID='" + _ItemGuidID + "'");
            List<MagazineTOC> tocs = MagazineTOC.MagazineTOCCollection(null, conditions);
            foreach (MagazineTOC toc in tocs)
            {
                ListViewItem lvi = new ListViewItem(toc.Title);
                lvi.SubItems.Add(toc.Author);
                lvi.SubItems.Add(toc.Page.ToString());
                lvToc.Items.Add(lvi);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            MagazineTOCInfo toc = new MagazineTOCInfo();
            toc.TocAdded += new MagazineTOCInfoEventHandler(toc_TocAdded);
            toc.IsNew = true;
            toc.ItemGuidID = ItemGuidID;
            toc.ShowDialog();
        }

        void toc_TocAdded(object sender, MagazineTOCInfoEventArgs e)
        {
            //add to the listview
            ListViewItem lvi = new ListViewItem(e.TOC.Title);
            lvi.SubItems.Add(e.TOC.Author);
            lvi.SubItems.Add(e.TOC.Page.ToString());
            lvToc.Items.Add(lvi);
        }

        private void lbAddNewPublisher_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PublisherForm pubForm = new PublisherForm();
            pubForm.PublisherAdded += new PublisherFormEventHandler(pubForm_PublisherAdded);
            pubForm.ShowDialog();
        }

        void pubForm_PublisherAdded(object sender, EventArgs e)
        {
            if (Program.PublisherCollection != null)
            {
                Program.PublisherCollection.Clear();
                Program.PublisherCollection = null;
            }
            LoadPublisherList();
        }
    }
}
